package com.gxa.controller;

import com.gxa.common.PaginationResult;
import com.gxa.common.ResponseData;
import com.gxa.pojo.*;
import com.gxa.service.CustomerService;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * User: zrf.
 * Date: 2020/10/26 21:51
 * Description:客户管理
 * Version: V1.0
 */
@Controller
@RequestMapping("/custom")
@Api(value = "客户管理",tags = "客户管理接口API")
public class CustomerController {

    @Autowired
    private CustomerService customerService;
    @Autowired
    private ResponseData responseData;

    @RequestMapping(value = "/selCustom", method = RequestMethod.GET)
    @ApiOperation(value = "查询客户资料", notes = "查询客户资料", httpMethod = "get", response = ResponseData.class)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "currentPage", value = "当前页码数", paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "每页显示条数", paramType = "query"),
    })
    @ResponseBody
    public ResponseData selCustom(@RequestParam(defaultValue = "1") Integer currentPage, @RequestParam(defaultValue = "5") Integer pageSize) {
        System.out.println(currentPage + "  " + pageSize);
        PaginationResult paginationResult = customerService.selCustom(currentPage,pageSize);
        if(paginationResult.getData() != null){
            responseData.setCode(200);
            responseData.setMsg("成功");
            responseData.setData(paginationResult);
        }else{
            responseData.setCode(400);
            responseData.setMsg("失败");
            responseData.setData(null);
        }
        return responseData;
    }

    @RequestMapping(value = "/addCustom", method = RequestMethod.POST)
    @ApiOperation(value = "添加客户", notes = "添加客户", httpMethod = "post", response = ResponseData.class)
    @ResponseBody
    public ResponseData addCustom(@RequestBody @ApiParam(name = "membershipCard", value = "会员卡信息",required = true) MembershipCard membershipCard) {
        System.out.println(membershipCard.toString());
        int ruselt = customerService.addCustom(membershipCard);
        if(ruselt > 0){
            responseData.setCode(200);
            responseData.setMsg("成功");
            responseData.setData(null);
        }else{
            responseData.setCode(400);
            responseData.setMsg("失败");
            responseData.setData(null);
        }

        return responseData;
    }

    @RequestMapping(value = "/queryCustom", method = RequestMethod.POST)
    @ApiOperation(value = "编辑客户", notes = "编辑客户", httpMethod = "post", response = ResponseData.class)
    @ResponseBody
    public ResponseData queryCustom(@RequestBody @ApiParam(name = "membershipCard", value = "会员卡信息",required = true) MembershipCard membershipCard) {
        System.out.println(membershipCard.toString());
        int result = customerService.updateCustom(membershipCard);
        if(result > 0){
            responseData.setCode(200);
            responseData.setMsg("成功");
            responseData.setData(null);
        }else{
            responseData.setCode(400);
            responseData.setMsg("失败");
            responseData.setData(null);
        }


        return responseData;
    }

    @RequestMapping(value = "/updateMembershipCard", method = RequestMethod.POST)
    @ApiOperation(value = "会员升级", notes = "会员升级", httpMethod = "post", response = ResponseData.class)
    @ResponseBody
    public ResponseData updateMembershipCard(@RequestBody @ApiParam(name = "membershipCard", value = "会员卡信息",required = true) MembershipCard membershipCard) {
        System.out.println(membershipCard.toString());
        int result = customerService.updateMembershipCard(membershipCard);
        if(result > 0){
            responseData.setCode(200);
            responseData.setMsg("成功");
            responseData.setData(null);
        }else{
            responseData.setCode(400);
            responseData.setMsg("失败");
            responseData.setData(null);
        }

        return responseData;
    }

    @RequestMapping(value = "/delCupro", method = RequestMethod.GET)
    @ApiOperation(value = "删除会员", notes = "删除会员", httpMethod = "get", response = ResponseData.class)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "memberId", value = "会员卡号", paramType = "query")
    })
    @ResponseBody
    public ResponseData delCupro(@RequestParam Integer memberId) {
        System.out.println(memberId);
        int result = customerService.delCupro(memberId);
        if(result > 0 ){
            responseData.setCode(200);
            responseData.setMsg("成功");
            responseData.setData(null);
        }else{
            responseData.setCode(400);
            responseData.setMsg("失败");
            responseData.setData(null);
        }
        return responseData;
    }

    @RequestMapping(value = "/queryCupro", method = RequestMethod.GET)
    @ApiOperation(value = "搜索会员", notes = "根据卡号搜索会员", httpMethod = "get", response = ResponseData.class)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "memberId", value = "会员卡号", paramType = "query"),
    })
    @ResponseBody
    public ResponseData queryCupro(@RequestParam Integer memberId) {
        System.out.println(memberId);
        List<Map> mapList = customerService.selMem(memberId);
        if(mapList != null){
            responseData.setCode(200);
            responseData.setMsg("成功");
            responseData.setData(mapList);
        }else{
            responseData.setCode(400);
            responseData.setMsg("用户不存在");
            responseData.setData(null);
        }


        return responseData;
    }

    @RequestMapping(value = "/selectMember", method = RequestMethod.GET)
    @ApiOperation(value = "查询会员卡", notes = "查询会员卡", httpMethod = "get", response = ResponseData.class)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "currentPage", value = "当前页码数", paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "每页显示条数", paramType = "query")
    })
    @ResponseBody
    public ResponseData selectMember(@RequestParam(defaultValue = "1") Integer currentPage, @RequestParam(defaultValue = "5") Integer pageSize) {
        System.out.println(currentPage + " " + pageSize);
        PaginationResult paginationResult = customerService.seleMember(currentPage,pageSize);
        if(paginationResult.getData() != null){
            responseData.setCode(200);
            responseData.setMsg("成功");
            responseData.setData(paginationResult);
        }else{
            responseData.setCode(400);
            responseData.setMsg("失败");
            responseData.setData(null);
        }
        return responseData;
    }

    @RequestMapping(value = "/addMember", method = RequestMethod.POST)
    @ApiOperation(value = "增加会员卡", notes = "增加会员卡", httpMethod = "post", response = ResponseData.class)
    @ResponseBody
    public ResponseData addMember(@RequestBody @ApiParam(name = "membershipCardType", value = "会员卡类型信息",required = true) MembershipCardType membershipCardType) {
        System.out.println(membershipCardType.toString());
        int result = customerService.addMember(membershipCardType);
        if(result > 0){
            responseData.setCode(200);
            responseData.setMsg("成功");
            responseData.setData(null);
        }else{
            responseData.setCode(400);
            responseData.setMsg("失败");
            responseData.setData(null);
        }
        return responseData;
    }

    @RequestMapping(value = "/updateMember", method = RequestMethod.POST)
    @ApiOperation(value = "编辑会员卡", notes = "编辑会员卡", httpMethod = "post", response = ResponseData.class)
    @ResponseBody
    public ResponseData updateMember(@RequestBody @ApiParam(name = "membershipCardType", value = "会员卡类型信息",required = true) MembershipCardType membershipCardType) {
        System.out.println(membershipCardType.toString());
        int result = customerService.updateMemb(membershipCardType);
        if(result > 0){
            responseData.setCode(200);
            responseData.setMsg("成功");
            responseData.setData(null);
        }else{
            responseData.setCode(400);
            responseData.setMsg("失败");
            responseData.setData(null);
        }
        return responseData;
    }

    @RequestMapping(value = "/deleteMember", method = RequestMethod.GET)
    @ApiOperation(value = "删除会员卡", notes = "删除会员卡", httpMethod = "get", response = ResponseData.class)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "membershipCardId", value = "会员卡id", paramType = "query")
    })
    @ResponseBody
    public ResponseData deleteMember(@RequestParam Integer membershipCardId) {
        System.out.println(membershipCardId);
        int result = customerService.delMemb(membershipCardId);
        if(result > 0){
            responseData.setCode(200);
            responseData.setMsg("成功");
            responseData.setData(null);
        }else{
            responseData.setCode(400);
            responseData.setMsg("失败");
            responseData.setData(null);
        }
        return responseData;
    }

    @RequestMapping(value = "/selMembTyp", method = RequestMethod.GET)
    @ApiOperation(value = "查询会员类型类型", notes = "查询会员类型", httpMethod = "get", response = ResponseData.class)
    @ResponseBody
    public ResponseData selMembTyp(){
        List<Map> mapList = customerService.selMembTyp();
        if(mapList != null){
            responseData.setData(mapList);
            responseData.setCode(200);
            responseData.setMsg("成功");
        }else{
            responseData.setCode(400);
            responseData.setMsg("失败");
            responseData.setData(null);
        }
        return responseData;
    }


    @RequestMapping(value = "/selCoditClass", method = RequestMethod.GET)
    @ApiOperation(value = "查询开卡门店", notes = "查询开卡门店", httpMethod = "get", response = ResponseData.class)
    @ResponseBody
    public ResponseData selCoditClass(){
        List<Map> mapList = customerService.selBreach();
        responseData.setData(mapList);
        responseData.setCode(200);
        responseData.setMsg("成功");
        return responseData;
    }
}

